Ext.define('Holo.controller.WindowsNewSpot', {
    extend: 'Ext.app.Controller',
	
    views: [
        'spot.WindowsNewSpot'
    ],
    
    stores: [
        'Spots',
    ],
    
    
    refs: [
            {
                selector: '#WindowsNewSpotForm',
                ref: 'formNewSpot'
            },{
                selector: 'windowsnewspot',
                ref: 'WindowsNewSpot'
            }
    ],



    
    init: function() {
    	 this.control({
                'windowsnewspot' :{
		    //afterrender : this.affectKeyMap
                    afterlayout : function (form){
                        GlobalData.currentController = this;
                        //console.log(this.getSpotsStore().count());
                        //form.down('textfield').focus();
                    } 
                },
                
	        'windowsnewspot button[action=createspot]': {
		    click : this.createNewSpot
                }  
        
        });
    },
    
//    /**
//    * Call createNewSpot() when the user push enter
//    * @return void
//    */
//    affectKeyMap : function(){
//    	
//    	var controller = this;	
//    	var nav = Ext.create('Ext.util.KeyNav', Ext.getBody(), {
//		    "enter" : function(){
//		        controller.createNewSpot();
//		    }
//		  }); 
//    
//    },
    
    createNewSpot : function(){
        var newSpotData = this.getFormNewSpot().getValues();
	if(newSpotData.name!=''){
	    try{
		var newSpot = Ext.create('Holo.model.Spot', {
					    name:newSpotData.name,
					    type: 'A',
					    status: 'B',
					    user_id : GlobalData.currentUserId
					});
	    
		this.getSpotsStore().add(newSpot);
	    }catch(err){
		console.log('erreur : controller/WindowsNewSpot.createNewSpot() =>'+ err.message);
	    }
	}
    },
    
    screenUpdate : function(){
            
        var controllerMenu = this.getController('MainMenu');
        controllerMenu.setItemsToSelectSpotMenu();
        controllerMenu.showSpotManager();
        
        this.getWindowsNewSpot().close();   
        
    }
  
 });